import store from '@/store'

function checkPermission (el, binding) {
  const { value } = binding
  const roles = store.getters && store.getters.roles

  if (value && value instanceof Array) {
    if (value.length > 0) {
      const permissionRoles = value

      const hasPermission = roles.some(role => {
        return permissionRoles.includes(role)
      })

      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    }
  } else {
    throw new Error(`need roles! Like v-permission="['admin','editor']"`)
  }
}
function compareVersion (version1, version2) {
  // ... 这里写代码实现
  if (!version1 || !version2) {
    return 0
  }
  const v1 = version1.split('.').map(d => parseInt(d, 10));
  const v2 = version2.split('.').map(d => parseInt(d, 10));
  let res = -2;
  for (let i = 0; i < v1.length; i++) {
    if (i < v2.length) {
      if (v1[i] < v2[i]) {
        res = -1;
        break;
      }
      else if (v1[i] > v2[i]) {
        res = 1;
        break;
      }
      else {
        const j = i + 1;
        if (j >= v1.length && j < v2.length) {
          if (v2[j] === 0) {
            res = 0;
          }
          else {
            res = -1;
          }
        }
      }
    }
    else {
      if (v1[i] === 0) {
        res = 0;
      }
      else {
        res = 1;
      }
      break;
    }
  }
  console.log(res);
}
compareVersion('0.1', '1.1.1'); // 返回-1
compareVersion('13.37', '1.2 '); // 返回1
compareVersion('1.1', '1.1.0'); // 返回0
compareVersion('1.1', '1.1.1'); // 返回-1

export default {
  inserted (el, binding) {
    checkPermission(el, binding)
  },
  update (el, binding) {
    checkPermission(el, binding)
  }
}
